﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.MobileControls;
using DevExpress.Xpo.Metadata;
using DevExpress.Xpo;
using System.Collections.Generic;
using OT.Common;
using DevExpress.Data.Filtering;
using OT.Dev;

public partial class Ajax_ComboData : DevPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 获得传递过来的参数
        string mainId = Request["mainId"];
        string mainType = Request["mainType"];
        string subType = Request["subType"];

        // 根据参数获取子表过滤数据
        string ret = GetDatas(mainId, mainType, subType);

        // 返回格式化的过滤数据
        Response.Write(ret);

        // 结束
        Response.End();
    }

    private string GetDatas(string mainId, string mainType, string subType)
    {
        XPClassInfo mainInfo = GetClassInfo ( mainType );
        XPClassInfo subInfo = GetClassInfo( subType );

        if ( mainInfo == null || subInfo == null )
            return string.Empty ;

        // 获得子表中关联主表的外键字段对应的表字段名
        string forigenKey = GetForigenKey ( mainInfo , subInfo );

        XPCollection xp = new XPCollection(XpoSession, subInfo.ClassType);
        xp.Criteria = new BinaryOperator(forigenKey, mainId);

        string ret = "";
        string div = "";
        foreach (object obj in xp)
        {
            // 获得ID值
            string id = (string)subInfo.GetId(obj);
            ret += string.Format("{0}{1},{2}", div, id, obj.ToString());

            div = ";";
        }

        return ret;
    }

    private XPClassInfo GetClassInfo(string mainType)
    {
        XPClassInfo retInfo = null ;
        // TODO:这里应当使用缓存进行加速
        XPDictionary dict = XpoDefault.DataLayer.Dictionary;
        foreach (XPClassInfo cinfo in dict.Classes)
        {
            if (mainType == cinfo.FullName)
            {
                retInfo = cinfo;
                break;
            }

            string className = GetClassName(cinfo.FullName);
            if (mainType == className)
            {
                retInfo = cinfo;
                break;
            }
        }

        return retInfo;
    }

    private string GetClassName(string fullName)
    {
        string className = "" ;

        int index = fullName.LastIndexOf(".");
        if (index < 0)
        {
            className = fullName;
        }
        else
        {
            className = fullName.Substring(index + 1);
        }

        return className;
    }

    private string GetForigenKey(XPClassInfo mainInfo, XPClassInfo subInfo)
    {
        // 获得主表中关联的子表字段
        IEnumerator ass = mainInfo.AssociationListProperties.GetEnumerator();

        string ret = "";
        while (ass.MoveNext())
        {
            ReflectionMemberInfo obj = ass.Current as ReflectionMemberInfo;

            XPMemberInfo subMember = obj.GetAssociatedMember();

            if (subMember.Owner.FullName == subInfo.FullName)
            {
                ret = subMember.Name;
            }
        }

        return ret;
    }
}
